/*
 * @Author: hanqifeitb 8592865+hanqifeitb@user.noreply.gitee.com
 * @Date: 2025-03-09 00:13:17
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2025-04-28 17:29:20
 * @FilePath: /tianmu/nuxt-font/utils/request.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from "axios";
import { getStorage } from "@/utils/index";
import { createPinia } from 'pinia';
import {  useUserStore }  from '@/stores/User'
import { ElMessage } from 'element-plus'

// 创建一个 axios 实例
const service = axios.create({
  baseURL:
    "https://admin.testace.cn/fenglingClient", // 线上
    //  process.env.NODE_ENV == "development" ? "http://10.6.40.37:8014" : "", // 本地
  timeout: 50000,
});

service.interceptors.request.use(
  (config) => {
    const token = useUserStore().userInfo?.token;
    if (token) {
      config.headers["token"] = `${token}`;
    }
    config.headers["shopid"] = 2;
    return config;
  },
  (error) => {
    console.error(error);
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  (response) => {
    const res = response.data;
    if (res.code !== '000000') {
      // ElMessage({
      //   message: res.info,
      //   type: 'error',
      //   plain: true,
      // })
      // console.error("Error:", res.message);
      if(res.code=='200000' || res.code=='200001'){
        // window.location.href="/login"
        window.localStorage.clear();
        console.error(res)
        return res;
      }
      return res;
    } else {
      return res;
    }
  },
  (error) => {
    console.error("Response Error:", error);
    return Promise.reject(error);
  }
);

export function get(url, params) {
  return service.get(url, { params });
}

export function post(url, data) {
  return service.post(url, data);
}

export function put(url, data) {
  return service.put(url, data);
}

export function del(url, params) {
  return service.delete(url, { params });
}

export default service;
